package cn.iocoder.yudao.module.handguard.service.productaccesspercentage;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.admin.productaccesspercentage.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.productaccesspercentage.ProductAccessPercentageDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.productaccesspercentage.ProductAccessPercentageConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.productaccesspercentage.ProductAccessPercentageMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 准入产品-提成参数 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ProductAccessPercentageServiceImpl implements ProductAccessPercentageService {

    @Resource
    private ProductAccessPercentageMapper productAccessPercentageMapper;

    @Override
    public Long createProductAccessPercentage(ProductAccessPercentageCreateReqVO createReqVO) {
        // 插入
        ProductAccessPercentageDO productAccessPercentage = ProductAccessPercentageConvert.INSTANCE.convert(createReqVO);
        productAccessPercentageMapper.insert(productAccessPercentage);
        // 返回
        return productAccessPercentage.getId();
    }

    @Override
    public void updateProductAccessPercentage(ProductAccessPercentageUpdateReqVO updateReqVO) {
        // 校验存在
        validateProductAccessPercentageExists(updateReqVO.getId());
        // 更新
        ProductAccessPercentageDO updateObj = ProductAccessPercentageConvert.INSTANCE.convert(updateReqVO);
        productAccessPercentageMapper.updateById(updateObj);
    }

    @Override
    public void deleteProductAccessPercentage(Long id) {
        // 校验存在
        validateProductAccessPercentageExists(id);
        // 删除
        productAccessPercentageMapper.deleteById(id);
    }

    private void validateProductAccessPercentageExists(Long id) {
        if (productAccessPercentageMapper.selectById(id) == null) {
            throw exception(PRODUCT_ACCESS_PERCENTAGE_NOT_EXISTS);
        }
    }

    @Override
    public ProductAccessPercentageDO getProductAccessPercentage(Long id) {
        return productAccessPercentageMapper.selectById(id);
    }

    @Override
    public List<ProductAccessPercentageDO> getProductAccessPercentageList(Collection<Long> ids) {
        return productAccessPercentageMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProductAccessPercentageDO> getProductAccessPercentagePage(ProductAccessPercentagePageReqVO pageReqVO) {
        return productAccessPercentageMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProductAccessPercentageDO> getProductAccessPercentageList(ProductAccessPercentageExportReqVO exportReqVO) {
        return productAccessPercentageMapper.selectList(exportReqVO);
    }

    @Override
    public ProductAccessPercentageDO getProductAccessPercentageByOrderId(Long id) {
        LambdaQueryWrapperX<ProductAccessPercentageDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(ProductAccessPercentageDO::getProductAccessId,id);
        ProductAccessPercentageDO productAccessPercentageDO = productAccessPercentageMapper.selectOne(queryWrapper);
        if (productAccessPercentageDO==null){
            productAccessPercentageDO = new ProductAccessPercentageDO();
            productAccessPercentageDO.setDeductType("0");
        }
        return productAccessPercentageDO;
    }

    @Override
    public ProductAccessPercentageDO getProductAccessPercentageByAccessId(Long accessId) {
        //查询提成参数
        LambdaQueryWrapperX<ProductAccessPercentageDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eq(ProductAccessPercentageDO::getProductAccessId,accessId);
        ProductAccessPercentageDO percentageDO = productAccessPercentageMapper.selectOne(queryWrapperX);
        return percentageDO;
    }

    @Override
    public void saveOrUpdateProductAccessPercentage(ProductAccessPercentageUpdateReqVO updateReqVO) {
        if (updateReqVO.getId()!=null){
            // 校验存在
            validateProductAccessPercentageExists(updateReqVO.getId());
            // 更新
            ProductAccessPercentageDO updateObj = ProductAccessPercentageConvert.INSTANCE.convert(updateReqVO);
            productAccessPercentageMapper.updateById(updateObj);
        }else {
            ProductAccessPercentageCreateReqVO createReqVO = new ProductAccessPercentageCreateReqVO();
            BeanUtils.copyProperties(updateReqVO,createReqVO);
            // 插入
            ProductAccessPercentageDO productAccessPercentage = ProductAccessPercentageConvert.INSTANCE.convert(createReqVO);
            productAccessPercentageMapper.insert(productAccessPercentage);
        }
    }

}
